/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __K_TICK_H__
#define __K_TICK_H__

/* includes (standard library, system) ---------------------------------------*/
#include <stdint.h>
#include <limits.h>
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
#include <x_list.h>
/* includes (local) ----------------------------------------------------------*/

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* defines -------------------------------------------------------------------*/

#define NO_WAIT      (0)
#define WAIT_FOREVER (UINT_MAX)

/* typedefs ------------------------------------------------------------------*/

struct tick_q_node {
    dlist_t node;
    unsigned int ticks_left;
    void (*pfn)(struct tick_q_node *, uintptr_t);
    uintptr_t arg;
    dlist_t *head; /* for safe deletion */
};

/* macro ---------------------------------------------------------------------*/
/* inlines -------------------------------------------------------------------*/
/* externs -------------------------------------------------------------------*/

extern unsigned long tick_tsc(void);
extern unsigned long tick_tsc_period(void);
extern unsigned long tick_tsc_freq(void);
extern void tick_q_del(struct tick_q_node *node);
extern void tick_q_add(struct tick_q_node *node, unsigned int ticks,
                       void (*pfn)(struct tick_q_node *, uintptr_t), uintptr_t arg);
extern uint32_t tick_count_get(void);
extern uint64_t tick_count_get64(void);
extern uint32_t tick_ms(void);
extern uint32_t tick_ms2tick(uint32_t ms);
extern uint32_t tick_tick2ms(uint32_t ticks);
extern void tick_hook_set(void (*sleep)(void), void (*awake)(void));

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __K_TICK_H__ */
